/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.ban.news.server.util.tracer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.util.HashMap;

/**
 *
 * @author zwlin
 */
public class DataTraceAgent implements DataTracer {

	public DataTraceAgent() {
		tracerMap = new HashMap<>();
	}

	private final HashMap<String, DataTracer> tracerMap;

	public void reg(String name, DataTracer tracer) {
		tracerMap.put(name, tracer);
	}

	@Override
	public void traceJsonParseError(String key, String content, long recvTimeSec) throws Exception {
		if (tracerMap.isEmpty()) {
		} else {
			for (DataTracer tracer : tracerMap.values()) {
				tracer.traceJsonParseError(key, content, recvTimeSec);
			}
		}
	}

	@Override
	public void traceMD5Error(String key, String content, long recvTimeSec) throws Exception {
		if (tracerMap.isEmpty()) {
		} else {
			for (DataTracer tracer : tracerMap.values()) {
				tracer.traceMD5Error(key, content, recvTimeSec);
			}
		}
	}

	@Override
	public void traceCorrect(String key, JSONObject srcPac, JSON trackRecList, int totalCount, long recvTimeSec) throws Exception {
		if (tracerMap.isEmpty()) {
		} else {
			for (DataTracer tracer : tracerMap.values()) {
				tracer.traceCorrect(key, srcPac, trackRecList, totalCount, recvTimeSec);
			}
		}
	}

	@Override
	public void traceJsonCheckError(String key, JSONObject srcPac, JSON trackRecList, int totalCount, int errorCount, long recvTimeSec) throws Exception {
		if (tracerMap.isEmpty()) {
		} else {
			for (DataTracer tracer : tracerMap.values()) {
				tracer.traceJsonCheckError(key, srcPac, trackRecList, totalCount, errorCount, recvTimeSec);
			}
		}
	}

	@Override
	public String getTracerDetail() {
		if (tracerMap.isEmpty()) {
			return "no tracers.";
		} else {
			String rtn = tracerMap.size() + " tracers:";
			for (DataTracer t : tracerMap.values()) {
				rtn += t.getTracerDetail() + " ";
			}
			return rtn;
		}

	}

}
